//
//  ZXHomeViewModel.swift
//  loveBee
//
//  Created by zhangxing on 2017/8/13.
//  Copyright © 2017年 zhangXing. All rights reserved.
//

import UIKit
import YYModel
class ZXHomeViewModel: NSObject {
    
    //无限轮播模型数组
    var cycleScrollModelList: [ZXCycleScrollModel]?
    
    //优惠模型数组
    var iconModelList: [ZXIconModel]?
    
    //新年惠活动模型数组
    var activityModelList: [ZXActivityModel]?

    //首页cell的viewModel数组(本来这里定义的是一个[ZXHomeCellModel]类型的模型数组,但是我们希望把数据传递给viewModel,所以在我们获取到网络的模型数组后,我们遍历这个模型数组,将其转成viewModel类型的数组,在这个ZXHomeCellViewModel中我们去做逻辑判断)
    var homeCellViewModelList: [ZXHomeCellViewModel] = [ZXHomeCellViewModel]()
    
    
    //  MARK: --   组头加载模型数据
    func loadData(callBack: @escaping(Bool) -> ()) {
        
        ZXNetWorkingManager.sharedManager.gainAppInfo(URLStr: "http://iosapi.itcast.cn/loveBeen/focus.json.php", call: "1") { (response, error) in
            if error != nil {
                
                print(error!)
                print("错误")
                //如果数据获取未成功,就回调失败的闭包
                callBack(false)
            }
            else {
                
                //解析数据
//                print(response!)
                
        /*
        {
            code = 0;
            data =  {
                activities=(
                                 {
                                 customURL = "https://github.com/ZhongTaoTian";
                                 id = 20575;
                                 img = "http://img01.bqstatic.com/upload/activity/activity_v4_20575_1452217080_block.jpg@90Q";
                                 name = "\U5e74\U8d27\U5927\U96c6";
                                 },
                                                            
                          );
                 
                 
                 
                 
                 focus =(
                             {
                             id = 20575;
                             img = "http://img01.bqstatic.com/upload/activity/activity_v4_20575_1452217080_focus.jpg@90Q";
                             name = "\U5e74\U8d27\U5927\U96c6";
                             toURL = "http://www.sfbest.com/html/zt/xianguoxinyu/index.html?device=2";
                             },
                        );
                 
                 
                 icons =(
                             {
                             customURL = "http://m.beequick.cn/show/active?id=20206&locationHash=7ca258Avt44fDvvj2cZdEGf4nzjyAg95fITJXcnfccCKbDJW6SdFHgelg&zchtid=10309&location_time=1453779210&show_reload=1&hide_cart=1&activitygroup=zhuanpan&name=%E6%8A%BD%E5%A5%96&bigids=2%2C0&cityid=2&addrid=4825569&zchtauth=0660c8G2eHuPa14mhxZ%252FCS4sObBfhTVY9vm1%252BSnWminqivhA&__v=ios4.10&__d=d14ryS0MFUAhfrQ6rPJ9Gziisg%2F9Cf8CxgkzZw5AkPMbPcbv%2BpM4HpDJlnwAZPd5UyoGA15VrRjnhCT8VdSGZRr1hqySmO%2FA2k1rqKaujWCJ69O%2BOlzJ%2FM3c%2BEAqx14eK17f";
                             id = 20206;
                             img = "http://img01.bqstatic.com/upload/activity/activity_v4_20206_1452163603_icon.jpg";
                             name = "\U62bd\U5956";
                             },
                      );
                 };
            msg = success;
       }

                 
            */
                //接下来就是解析数据
                let data = (response as! [String: Any])["data"] as! [String: Any]
                
                //得到字典数组
                let focus = data["focus"] as! [[String: Any]]
                
                //字典数组转模型数组
                self.cycleScrollModelList = NSArray.yy_modelArray(with: ZXCycleScrollModel.self, json: focus) as? [ZXCycleScrollModel]
                 

                let icons = data["icons"] as! [[String: Any]]
                
                //数组转模型
                self.iconModelList = NSArray.yy_modelArray(with: ZXIconModel.self, json: icons) as? [ZXIconModel]
                 
               
                let activities = data["activities"] as! [[String: Any]]
                
                //数组转模型
                self.activityModelList = NSArray.yy_modelArray(with: ZXActivityModel.self, json: activities) as? [ZXActivityModel]
                
                
                //如果数据获取成功,就回调成功的闭包
                callBack(true)
 
            }
        }
    }
    
    
    //  MARK: --   首页cell数据加载
    func loadCellData(callBack: @escaping(Bool) -> ()) {
        ZXNetWorkingManager.sharedManager.gainAppInfo(URLStr: "http://iosapi.itcast.cn/loveBeen/firstSell.json.php", call: "2") { (response, error) in
            if error != nil {
                
                print(error!)
                print("错误")
                
                //回调错误闭包
                callBack(false)
            }
            else {
                
//                print(response!)
                
                /*
                 {
                 code = 0;
                 data = (
                         {
                             attribute = "";
                             "brand_id" = 128;
                             "brand_name" = "\U7231\U9c9c\U8702";
                             "cart_group_id" = 0;
                             "category_id" = 106;
                             cid = 106;
                             "dealer_id" = 7951;
                             "had_pm" = 1;
                             "hot_degree" = 0;
                             id = 94576;
                             img = "http://img01.bqstatic.com/upload/goods/000/009/4576/0000094576_34960.jpg@300w_300h_90Q.jpg";
                             "is_del" = 0;
                             "is_xf" = 1;
                             ismix = 0;
                             "market_price" = "12.00";
                             name = "\U7231\U9c9c\U8702\U00b7\U6c5f\U897f\U8d21\U6854";
                             number = 3;
                             "partner_price" = "9.90";
                             pcid = 55;
                             "pm_desc" = "\U4e70\U4e00\U8d60\U4e00";
                             "pre_img" = "0000094576_34960.jpg";
                             "pre_imgs" = "0000094576_27260.jpg,";
                             price = "9.90";
                             "product_id" = 94576;
                             "safe_day" = 0;
                             sort = 1;
                             "source_id" = 1;
                             specifics = "400g/\U76d2";
                             "store_nums" = 50;
                             "tag_ids" = 5;
                         },
                        )
                 }
                 */
                
                //代码执行到此,数据加载成功
                //解析数据
                //response是一个字典,根据data这个键值取出来的是一个字典数组
                let data = (response as! [String: Any]) ["data"] as! [[String: Any]]
                
                //yy_model转成模型数组
                let modelList = NSArray.yy_modelArray(with: ZXHomeCellModel.self, json: data)
                
                //遍历数据存到viewModelList中
                for model in modelList! {
                    
                    //每遍历一次模型数组,我们就创建一个viewModel
                    let cellViewModel = ZXHomeCellViewModel()
                    
                    cellViewModel.homeCellModel = model as? ZXHomeCellModel
                    
                    self.homeCellViewModelList.append(cellViewModel)
                }
                
                //回调成功闭包
                callBack(true)
            }
        }
        
    }
    
    
    
}
